<?php

namespace app\admin\model\mental;

use think\Model;


class Extension extends Model
{



    protected $resultSetType = 'collection';

    // 表名
    protected $name = 'mental_extension';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'channel_type_text',
        'status_text',
        // 'qrcode',
        // 'order_price',
        // 'koudan_price'
    ];

    protected static function init()
    {
        self::event('before_insert', function ($model) {
        });
    }

    // public function getKoudanPriceAttr($value,$data)
    // {
    //     return \think\Db::name('koudan_log')->where(['extension_id'=>$data['id']])->sum('price');
    // }

    // public function getOrderPriceAttr($value,$data)
    // {
    //     return \think\Db::name('mental_order')->where(['extensionId'=>$data['id'],'status'=>'paid'])->sum('amount');
    // }

    // public function getUrlAttr($value, $data)
    // {
    //     $url = config('site.qdDomain') . '/pages/cpxq?id=' . $data['main_id'] . '&fxId=' . $data['id'];

    //     return $url;
    // }

    // public function getQrcodeAttr($value, $data)
    // {
    //     require_once THINK_PATH . 'phpqrcode.php';
    //     $url = config('site.qdDomain') . '/pages/cpxq?id=' . $data['main_id'] . '&fxId=' . $data['id'];
    //     ob_start();
    //     //生成二维码图片
    //     $returnData = \QRcode::pngString($url, false, "H", 3, 1);
    //     //这里就是把生成的图片流从缓冲区保存到内存对象上，使用base64_encode变成编码字符串，通过json返回给页面。
    //     $imageString = base64_encode(ob_get_contents());
    //     //关闭缓冲区
    //     ob_end_clean();
    //     $str = "data:image/png;base64," . $imageString;
    //     return $str;
    // }

    public function getChannelTypeList()
    {
        return [
            '1' => __('Channel_type 1'),
            '2' => __('Channel_type 2'),
            '3' => __('Channel_type 3'),
            '4' => __('Channel_type 4'),
            '5' => __('Channel_type 5'),
            '6' => __('Channel_type 6'),
            '7' => __('Channel_type 7'),
            '8' => __('Channel_type 8'),
            '9' => __('Channel_type 9'),
            '10' => __('Channel_type 10'),
            '11' => __('Channel_type 11'),
            '12' => __('Channel_type 12'),
            '13' => __('Channel_type 13'),
            '14' => __('Channel_type 14'),
            '15' => __('Channel_type 15'),
            '16' => __('Channel_type 16'),
            '17' => __('Channel_type 17'),
            '18' => __('Channel_type 18'),
            '19' => __('Channel_type 19'),
            '20' => __('Channel_type 20'),
            '21' => __('Channel_type 21'),
            '22' => __('Channel_type 22'),
            '23' => __('Channel_type 23'),
            '24' => __('Channel_type 24'),
            '25' => __('Channel_type 25'),
            '26' => __('Channel_type 26'),
            '27' => __('Channel_type 27'),
            '28' => __('Channel_type 28'),
            '29' => __('Channel_type 29'),
        ];
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }


    public function getChannelTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['channel_type']) ? $data['channel_type'] : '');
        $list = $this->getChannelTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function testmain()
    {
        return $this->belongsTo('app\admin\model\mental\TestMain', 'main_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
